package com.chuanye.jiguangimdemo.activitys;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.chuanye.jiguangimdemo.R;
import com.chuanye.jiguangimdemo.app.MyApplication;
import com.chuanye.jiguangimdemo.database.FriendEntry;
import com.chuanye.jiguangimdemo.database.FriendRecommendEntry;
import com.chuanye.jiguangimdemo.entity.Event;
import com.chuanye.jiguangimdemo.entity.EventType;



import java.util.ArrayList;
import java.util.List;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;
import cn.jpush.im.api.BasicCallback;

public class FriendSettingActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "FriendSettingActivity";
    private Context mContext;
    private RelativeLayout friesetting_notename_rl;
    private TextView friesetting_notename_tv;
    private RelativeLayout friesetting_businesscard_rl;
    private ToggleButton friesetting_addblacklist_tbtn;
    private Button friesetting_deleteFriend_btn;

    private UserInfo mFriendInfo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_setting);
        mContext = this;


        initView();
    }

    private void initView() {
        //返回
        ImageView friesetting_back_iv = findViewById(R.id.friesetting_back_iv);
        friesetting_back_iv.setOnClickListener(this);
        friesetting_notename_rl = findViewById(R.id.friesetting_notename_rl);//备注名布局
        friesetting_notename_rl.setOnClickListener(this);
        friesetting_notename_tv = findViewById(R.id.friesetting_notename_tv);//备注名
        friesetting_businesscard_rl = findViewById(R.id.friesetting_businesscard_rl);//发送名片
        friesetting_businesscard_rl.setOnClickListener(this);
        friesetting_addblacklist_tbtn = findViewById(R.id.friesetting_addblacklist_tbtn);//加入黑名单

        friesetting_deleteFriend_btn = findViewById(R.id.friesetting_deleteFriend_btn);//删除好友
        friesetting_deleteFriend_btn.setOnClickListener(this);

        friesetting_addblacklist_tbtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                String userName = getIntent().getStringExtra("userName");
                List<String> name = new ArrayList<>();
                name.add(userName);
                if (isChecked) {
                    //添加到黑名单
                    JMessageClient.addUsersToBlacklist(name, new BasicCallback() {
                        @Override
                        public void gotResult(int responseCode, String responseMessage) {
                            if (responseCode == 0) {
                                Toast.makeText(FriendSettingActivity.this, "添加成功",Toast.LENGTH_SHORT).show();
                            } else {
                                friesetting_addblacklist_tbtn.setChecked(false);
                                Toast.makeText(FriendSettingActivity.this, "添加失败" + responseMessage,Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                } else {
                    //取消到黑名单
                    JMessageClient.delUsersFromBlacklist(name, new BasicCallback() {
                        @Override
                        public void gotResult(int responseCode, String responseMessage) {
                            if (responseCode == 0) {
                                Toast.makeText(FriendSettingActivity.this, "移除成功",Toast.LENGTH_SHORT).show();
                            } else {
                                friesetting_addblacklist_tbtn.setChecked(true);
                                Toast.makeText(FriendSettingActivity.this, "移除失败" + responseMessage,Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        });


        if (!TextUtils.isEmpty(getIntent().getStringExtra("noteName"))) {
            friesetting_notename_tv.setText(getIntent().getStringExtra("noteName"));
        }
        //获取用户信息
        JMessageClient.getUserInfo(getIntent().getStringExtra("userName"), new GetUserInfoCallback() {
            @Override
            public void gotResult(int responseCode, String responseMessage, UserInfo info) {
                if (responseCode == 0) {
                    //设置请求的用户信息
                    mFriendInfo = info;
                    //设置是否是黑名单
                    friesetting_addblacklist_tbtn.setChecked(info.getBlacklist() == 1);
                    if (info.isFriend()) {//是否是好友
                        friesetting_deleteFriend_btn.setVisibility(View.VISIBLE);//显示删除好友按钮
                        friesetting_notename_rl.setVisibility(View.VISIBLE);//备注布局
                    }else {//不是好友
                        friesetting_deleteFriend_btn.setVisibility(View.GONE);//隐藏删除好友按钮
                        friesetting_notename_rl.setVisibility(View.GONE);//隐藏备注布局
                    }
                }
            }
        });

    }

    @SuppressLint("WrongConstant")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.friesetting_back_iv://返回
                finish();
                break;
            case R.id.friesetting_notename_rl://备注名
                Intent intent = new Intent(FriendSettingActivity.this, SetNoteNameActivity.class);
                intent.putExtra("user", getIntent().getStringExtra("userName"));
                intent.putExtra("note", getIntent().getStringExtra("noteName"));
                startActivityForResult(intent, 1);
                break;
            case R.id.friesetting_businesscard_rl://发送名片
                Intent businessIntent = new Intent(FriendSettingActivity.this, ForwardMsgActivity.class);
                businessIntent.setFlags(1);
                businessIntent.putExtra("userName", mFriendInfo.getUserName());
                businessIntent.putExtra("appKey", mFriendInfo.getAppKey());
                if (mFriendInfo.getAvatarFile() != null) {
                    businessIntent.putExtra("avatar", mFriendInfo.getAvatarFile().getAbsolutePath());
                }
                startActivity(businessIntent);
                break;
            case R.id.friesetting_deleteFriend_btn://删除好友

                mFriendInfo.removeFromFriendList(new BasicCallback() {
                    @Override
                    public void gotResult(int responseCode, String responseMessage) {
                        if (responseCode == 0) {
                            //将好友删除时候还原黑名单设置
                            List<String> name = new ArrayList<>();

                            name.add(mFriendInfo.getUserName());
                            //删除
                            JMessageClient.delUsersFromBlacklist(name, null);
                            FriendEntry friend = FriendEntry.getFriend(MyApplication.getUserEntry(), mFriendInfo.getUserName(), mFriendInfo.getAppKey());
                            if (friend != null) {
                                friend.delete();
                            }
                            FriendRecommendEntry entry = FriendRecommendEntry
                                    .getEntry(MyApplication.getUserEntry(),
                                            mFriendInfo.getUserName(), mFriendInfo.getAppKey());
                            if (entry != null) {
                                entry.delete();
                            }
                            Toast.makeText(FriendSettingActivity.this, "移除好友",Toast.LENGTH_SHORT).show();

                            delConvAndReturnMainActivity();
                        } else {
                            Toast.makeText(FriendSettingActivity.this, "移除失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && data != null) {
            friesetting_notename_tv.setText(data.getStringExtra("updateName"));
        }
    }

    public void delConvAndReturnMainActivity() {
        Conversation conversation = JMessageClient.getSingleConversation(mFriendInfo.getUserName(), mFriendInfo.getAppKey());
        /*EventBus.getDefault().post(new Event.Builder().setType(EventType.deleteConversation)
                .setConversation(conversation)
                .build());*/
        //删除会话
        JMessageClient.deleteSingleConversation(mFriendInfo.getUserName(), mFriendInfo.getAppKey());
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}
